//
//  NetworkingManager.m
//  网络练习
//
//  Created by 张令林 on 16/8/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "NetworkingManager.h"

@implementation NetworkingManager
#pragma mark - 生成网络连接访问的单例管理器
+ (instancetype)shareManager{
	static NetworkingManager *client = nil;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
//		client = [[NetworkingManager alloc]initWithBaseURL:nil];
//		client.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
//		//支持接收的文本类型 : text/html
//		client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
//		//AFN默认的成功的回调 responseObject 是把服务器返回的数据当做JSON数据格式解析,返回字典或者数组.
//		//修改AFN默认返回的数据类型,设置默认为只返回原始的二进制数据,程序猿自己解析
//		//client.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSURL *url = [NSURL URLWithString:@"http://iosapi.itcast.cn/loveBeen/"];
        
        client = [[NetworkingManager alloc]initWithBaseURL:url];
        client.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
	});
	return client;
}
#pragma mark - get方式的访问
+ (void)getUrlString:(NSString *)url
		   withParam:(NSDictionary *)param
	withSuccessBlock:(successBlock)success
	 withFailedBlock:(failedBlock)failed
	  withErrorBlock:(errorBlock)error{
	
	[[self shareManager] GET:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, NSDictionary  *responseObject) {
		//调用成功的block
		success(responseObject);
	} failure:^(NSURLSessionDataTask *task, NSError *error) {
		//调用失败的block
		failed(error);
	}];
}
#pragma mark - post方式的访问
+ (void)postUrlString:(NSString *)url
			withParam:(NSDictionary *)param
	 withSuccessBlock:(successBlock)success
	  withFailedBlock:(failedBlock)failed
	   withErrorBlock:(errorBlock)error{
	
	[[self shareManager] POST:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, NSDictionary  *responseObject) {
		//调用成功的block
		success(responseObject);
	} failure:^(NSURLSessionDataTask *task, NSError *error) {
		//调用失败的block
		failed(error);
	}];
}

@end


